﻿using System;
using System.IO;
using System.Windows.Forms;
using Mini.Model.BaseModels;
using Mini.Resource;
namespace Mini.Common
{
    public class DirectoryHelper
    {
        public static string CreateDirectoryDefault(string pageDirectory)
        {

            if (!IsDirectoryExits(pageDirectory))
            {
                CreateDirectory(pageDirectory);
            }

            var folderProject = Enum.GetValues(typeof(FolderType));
            foreach (var folder in folderProject)
            {
                var folderName = folder.GetCustomAttributeDescription().ToLower();
                var directoryName = Path.Combine(pageDirectory, folderName);
                if (!IsDirectoryExits(directoryName))
                {
                    CreateDirectory(directoryName);
                }
            }

            return pageDirectory;
        }
        public static string GetDirectoryDefault()
        {
            var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            return desktopPath;
        }
        public static void DeleteFolder(string path)
        {
            var downloadedMessageInfo = new DirectoryInfo(path);

            foreach (var file in downloadedMessageInfo.GetFiles())
            {
                file.Delete();
            }
            foreach (var dir in downloadedMessageInfo.GetDirectories())
            {
                dir.Delete(true);
            }
        }
        public static string CreatePageDirectory(string projectDirectory, string pageName)
        {
            var isProjectDirectory = IsDirectoryExits(projectDirectory);
            if (!isProjectDirectory)
                CreateDirectory(projectDirectory);

            var pageDirectory = Path.Combine(projectDirectory, pageName);
            var isPageDirectoryExits = IsDirectoryExits(pageDirectory);
            if (isPageDirectoryExits)
            {
                var result = MessageBox.Show(string.Format(MessageResource.WaringWebSiteExists, Environment.NewLine), @"Warning !", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (result == DialogResult.OK)
                {
                    DeleteFolder(projectDirectory);
                    CreateDirectoryDefault(pageDirectory);
                    return pageDirectory;
                }

                var newProjectName = RenameProjectName(pageName);
                return CreatePageDirectory(projectDirectory, newProjectName);
            }
            return CreateDirectoryDefault(pageDirectory);
        }
        public static string RenameProjectName(string projectName)
        {
            var random = new Random();
            var randomNumber = random.Next(0, 100);
            projectName = string.Format("{0}_{1}", projectName, randomNumber);
            var result = MessageBox.Show(string.Format(string.Format(MessageResource.QuestionCreateNewProject, Environment.NewLine), projectName), MessageResource.WarningTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.Cancel)
            {
                projectName = ProgramHelper.ShowDialog(MessageResource.MessageEnterProjectName, MessageResource.TitleMessage);
            }
            return projectName;
        }
        public static bool IsDirectoryExits(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }
        public static void CreateDirectory(string directoryPath)
        {
            Directory.CreateDirectory(directoryPath);
        }
        public static void CreateProjectFolder(string projectDirectory)
        {
            var isProjectDirectory = IsDirectoryExits(projectDirectory);
            if (!isProjectDirectory)
                CreateDirectory(projectDirectory);
        }

       
    }
}
